package com.umsoft.smart.framework.util;


/**
 * 转型操作工具类
 */
public class CastUtil {

    /**
     * 转为String 型
     * @param obj
     * @return
     */
    public static String castString(Object obj){
        return castString(obj,"");
    }

    /**
     * 转为String 型(提供默认值)
     * @param obj
     * @return
     */
    public static String castString(Object obj,String defalutValue){
        return obj == null ? defalutValue : String.valueOf(obj);
    }

    /**
     * 转为double 型(默认值为0)
     * @param obj
     * @return
     */
    public  static double castDouble(Object obj){
        return castDouble(obj,0);
    }

    /**
     * 转为 double 型(提供默认值)
     * @param obj
     * @return
     */
    private static double castDouble(Object obj, double defaultValue) {
        double value = defaultValue;
        if(obj != null){
            String strValue = castString(obj);
            if(StringUtil.isNotEmpty(strValue)){
                try {
                    value = Double.parseDouble(strValue);
                }catch (NumberFormatException ex){
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    /**
     * 转为 long 型(默认值为0)
     * @param obj
     * @return
     */
    public  static long castLong(Object obj){
        return castLong(obj,0);
    }

    /**
     * 转为 long 型(提供默认值)
     * @param obj
     * @return
     */
    private static long castLong(Object obj, long defaultValue) {
        long value = defaultValue;
        if(obj != null){
            String strValue = castString(obj);
            if(StringUtil.isNotEmpty(strValue)){
                try {
                    value = Long.parseLong(strValue);
                }catch (NumberFormatException ex){
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    /**
     * 转为 int 型(默认值为0)
     * @param obj
     * @return
     */
    public static   int castInt(Object obj){
        return castInt(obj,0);
    }

    /**
     * 转为 int 型(提供默认值)
     * @param obj
     * @return
     */
    private static int castInt(Object obj, int defaultValue) {
        int value = defaultValue;
        if(obj != null){
            String strValue = castString(obj);
            if(StringUtil.isNotEmpty(strValue)){
                try {
                    value = Integer.parseInt(strValue);
                }catch (NumberFormatException ex){
                    value = defaultValue;
                }
            }
        }
        return value;
    }

    /**
     * 转为 boolean 型(默认值为0)
     * @param obj
     * @return
     */
    public static boolean castBoolean(Object obj){
        return castBoolean(obj,false);
    }

    /**
     * 转为 boolean 型(提供默认值)
     * @param obj
     * @return
     */
    private static boolean castBoolean(Object obj, boolean defaultValue) {
        boolean value = defaultValue;
        if(obj != null){
            value = Boolean.parseBoolean(castString(obj));
        }
        return value;
    }

}
